// pages/orderDetail/orderDetail.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dishesArray: [], //订单中的菜数组
    canCancel: false,
    canComment: false,
    ifFinished: false,
    endTime:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options.orderId);
    var that = this;
    var _orderid = options.orderId;
    wx.request({
      url: app.globalData.ip + '/api/order/' + _orderid,
      method: "GET",
      header: {
        "X-Token": app.globalData.token
      },
      success: e => {
        var orderJson = e.data.data;
        var _canCancel = false;
        var _canComment = false;
        var _ifFinished = false;
        var _status;
        var _endTime="";
        if (orderJson.orderEntity.state == 0) {
          _status = "待接单";
          _canCancel = true;
        } else if (orderJson.orderEntity.state == 1)
          _status = "准备中";
        else if (orderJson.orderEntity.state == 2) {
          _canComment = true;
          _status = "待评价";
        } else if (orderJson.orderEntity.state == 3)
          _status = "已取消";
        else {
          _status = "已结束";
          _ifFinished = true;
          _endTime = app.timeFormat(orderJson.orderEntity.updateTime);
        }

        var _createTime = app.timeFormat(orderJson.orderEntity.createTime);
        that.setData({
          dishesArray: orderJson.list,
          orderId: orderJson.orderEntity.id,
          time: _createTime,
          endTime:_endTime,
          money: orderJson.orderEntity.totalAmount,
          state: orderJson.orderEntity.state,
          status: _status,
          canCancel: _canCancel,
          canComment: _canComment,
          ifFinished: _ifFinished
        })
      }
    })
  },
  cancelOrder: function(e) {
    var that = this
    wx.request({
      url: app.globalData.ip + '/api/order/' + that.data.orderId + "?state=cancel",
      method: "PUT",
      header: {
        "X-Token": app.globalData.token
      },
      success: e => {
        // console.log(e);
        wx.navigateTo({
          url: '../order/order',
        })
      }
    })
  },
  gotoComment: function(e) {
    var that = this;
    wx.navigateTo({
      url: '../comment/comment?orderId=' + that.data.orderId,
    })
  }
})